$(function () {
	// initFtpFileTable();
	initRedisDbMenu();
	layui.use('dropdown', function () {
		var dropdown = layui.dropdown;
		//菜单点击事件，其中 redisDbMenu 对应的是菜单结构上的 id 指
		dropdown.on('click(redisDbMenu)', function (options) {
			var othis = $(this); //当前菜单列表的 DOM 对象
			console.log(options); //菜单列表的 lay-options 中的参数
			redisValue(options);
		});
	});
});
var layer = layui.layer; //弹层
var ftpConnectState = false;
function ftpTest() {
	if ($("input[name='ip']").val() == '' || $("input[name='port']").val() == '') {
		layer.msg('请输入连接信息');
		return;
	}
	let load = openLoad();
	$.ajax({
		type: "POST",
		url: "/tool/index/ftpTest",
		data: $('#ftpForm').serialize(),
		// data: {},
		dataType: "JSON",
		success: function (result) {
			layer.close(load);
			if (result.code == 0) {
				ftpConnectState = true;
				layer.msg('连接成功');
			} else if (result.code == 500) {
				layer.alert("失败，原因：" + result.msg);
			}
		}
	});
}

function ftpFiles() {
	if (!ftpConnectState) {
		layer.msg('请先测试连接');
		return;
	}
	let load = openLoad();
	$.ajax({
		type: "POST",
		url: "/tool/index/ftpFiles",
		data: {},
		// data: {},
		dataType: "JSON",
		success: function (result) {
			layer.close(load);
			if (result.code == 0) {
				var fileRows = '';
				for (const file of result.data) {
					fileRows += `<tr><td>${file.fileName}</td><td>${file.fileTime}</td></tr>`;
				}
				$('#ftpFileTableTbody').html(fileRows);
			} else if (result.code == 500) {
				layer.alert("失败，原因：" + result.msg);
			}
		}
	});
}

function ftpUpload() {
	if (!ftpConnectState) {
		layer.msg('请先测试连接');
		return;
	}
	let load = openLoad();
	$.ajax({
		type: "POST",
		url: "/tool/index/ftpUpload",
		data: {},
		// data: {},
		dataType: "JSON",
		success: function (result) {
			layer.close(load);
			if (result.code == 0) {
				ftpFiles();
				layer.msg('操作成功');
			} else if (result.code == 500) {
				layer.alert("失败，原因：" + result.msg);
			}
		}
	});
}

function ftpDelete() {
	if (!ftpConnectState) {
		layer.msg('请先测试连接');
		return;
	}
	let load = openLoad();
	$.ajax({
		type: "POST",
		url: "/tool/index/ftpDelete",
		data: {},
		// data: {},
		dataType: "JSON",
		success: function (result) {
			layer.close(load);
			if (result.code == 0) {
				ftpFiles();
				layer.msg('操作成功');
			} else if (result.code == 500) {
				layer.alert("失败，原因：" + result.msg);
			}
		}
	});
}

function openLoad() {
	return layer.load(2, {shade: [0.4, '#393D49']});
}

var redisConnectState = false;

function redisTest() {
	if ($("input[name='host']").val() == '' || $("input[name='port']").val() == '') {
		layer.msg('请输入连接信息');
		return;
	}
	let load = openLoad();
	$.ajax({
		type: "POST",
		url: "/tool/index/redisTest",
		data: $('#redisForm').serialize(),
		// data: {},
		dataType: "JSON",
		success: function (result) {
			layer.close(load);
			if (result.code == 0) {
				redisConnectState = true;
				layer.msg('连接成功');
			} else if (result.code == 500) {
				layer.alert("失败，原因：" + result.msg);
			}
		}
	});
}

function initRedisDbMenu() {
	let dbMenus = '';
	for (let i = 0; i < 16; i++) {
		dbMenus += `<li class="layui-menu-item-group layui-menu-item-up" lay-options="{type: 'group'}" id="redisDb${i}">
						<div class="layui-menu-body-title">db${i}</div>
					</li>`;
	}
	$('#redisDbMenu').html(dbMenus);
}

function redisKeys() {
	if (!redisConnectState) {
		layer.msg('请先测试连接');
		return;
	}
	for (let i = 0; i < 16; i++) {
		$.ajax({
			type: "POST",
			url: "/tool/index/redisKeys",
			async: false,
			data: {index: i},
			dataType: "JSON",
			success: function (result) {
				if (result.code == 0 && result.data.length > 0) {
					$(`#redisDb${i}`).html('');
					$(`#redisDb${i}`).append(`<div class="layui-menu-body-title">db${i} <span class="layui-circle" style="background-color: #009688"> ${result.data.length} </span><i class="layui-icon layui-icon-down"></i></div>`);
					let ul = '<ul>';
					for (const key of result.data) {
						ul += `<li lay-options="{key: '${key}',index:${i}}" class="layui-elip" title="${key}">${key}</li>`;
					}
					ul += '</ul>';
					$(`#redisDb${i}`).append(ul);
					// alert(result.data);
				}
			}
		});
	}
}

function redisValue(options) {
	let load = openLoad();
	$.ajax({
		type: "POST",
		url: "/tool/index/redisValue",
		data: options,
		// data: {},
		dataType: "JSON",
		success: function (result) {
			layer.close(load);
			if (result.code == 0) {
				if (result.data.redisVoList.length > 0) {
					let thead = '<tr><th>value</th></tr>';
					if (result.data.type == 'hash') {
						thead = '<tr><th>field</th><th>value</th></tr>';
					}
					$('#redisTableThead').html(thead);
					var valueRows = '';
					for (const redisValue of result.data.redisVoList) {
						if (result.data.type == 'hash') {
							valueRows += `<tr><td>${redisValue.field}</td><td>${redisValue.value}</td></tr>`;
						} else {
							valueRows += `<tr><td>${redisValue.value}</td></tr>`;
						}
					}
					$('#redisTableTbody').html(valueRows);
				}
			} else if (result.code == 500) {
				layer.alert("失败，原因：" + result.msg);
			}
		}
	});
}